<?php


namespace App\Http\Controllers\Back;


use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class MediaController extends Controller
{
    public function index()
    {
        $users = DB::table('media_users')->select(['media_users.id',
            'media_users.phone',
            'media_user_info.account',
            'media_users.identity',
            'media_users.created_at',
            'media_users.status'
            ])->join('media_user_info', 'media_user_info.media_user_id', '=', 'media_users.id')->get();
        return $this->renderJson($users);
    }

    /**
     * 查看账户详情
     * @param $id
     * @return \Illuminate\Http\Response
     */
    public function media_user_info($id)
    {
        $user = DB::table('media_users')->where('id', $id)->first();
        $user_info = DB::table('media_user_info')->where('media_user_id', $id)->first();
        $data = [
            'account' => $user_info?$user_info->account:'',
            'introduce' => $user_info?$user_info->introduce:'',
            'avatar' => $user_info?$user_info->avatar:'',
            'operators_name' => $user_info?$user_info->operators_name:'',
            'operators_id_no' => $user_info?$user_info->operators_id_no:'',
            'operators_id_photo' => $user_info?$user_info->operators_id_photo:'',
            'address' => $user_info?$user_info->address:'',
            'email' => $user_info?$user_info->email:''
        ];
        //企业信息
        if ($user->identity == 2){
            $user_institution = DB::table('media_user_institution')->where('media_user_id')->first();

            $data['other_aptitude'] = $user_institution?$user_institution->other_aptitude:'';
            $data['aux_data'] = $user_institution?$user_institution->aux_data:'';
            $data['institution_name'] = $user_institution?$user_institution->institution_name:'';
            $data['institution_license'] = $user_institution?$user_institution->institution_license:'';
            $data['institution_confirmation'] = $user_institution?$user_institution->institution_confirmation:'';
            $data['institution_web_site'] = $user_institution?$user_institution->institution_web_site:'';
            $data['institution_address'] = $user_institution?$user_institution->institution_address:'';
        }
        return $this->renderJson($data);
    }

    /**
     * 认证
     * @param $id
     * @return \Illuminate\Http\Response
     */
    public function adopt($id)
    {
        DB::table('users')->where('id', $id)->update(['status'=>4, 'authentication_at'=>date('Y-m-d H:i:s')]);
        return $this->success();
    }

    /**
     * 不通过
     * @param $id
     * @return \Illuminate\Http\Response
     */
    public function reject($id)
    {
        DB::table('users')->where('id', $id)->update(['status'=>2]);
        return $this->success();
    }

    /**
     * 封禁
     * @param $id
     * @return \Illuminate\Http\Response
     */
    public function block($id)
    {
        DB::table('users')->where('id', $id)->update(['status'=>5,'block_at'=>date('Y-m-d H:i:s')]);
        return $this->success();
    }

    /**
     * 解封
     * @param $id
     * @return \Illuminate\Http\Response
     */
    public function unseal($id)
    {
        DB::table('users')->where('id', $id)->update(['status'=>6,'unseal_at'=>date('Y-m-d H:i:s')]);
        return $this->success();
    }
}